package com.qfedu.controller;

import com.qfedu.common.JsonBean;
import com.qfedu.pojo.SysUser;
import com.qfedu.service.SysUserService;
import com.qfedu.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


@Controller  //注解 用于实例化SysUserController,相当于实现Controller接口
@RequestMapping("/user")  //相当于在url后面加前缀
public class SysUserController {

    @Autowired //根据类型自动注入对象
    private SysUserService uerService;  //向上转型

    //登录
    @RequestMapping("/login.do")
    @ResponseBody   //返回给前端的是 json字符串 数据类型
    public JsonBean login(String tel, String password, HttpSession session){
        JsonBean bean=null;
        try {
            SysUser users = uerService.findByName(tel, password);
            session.setAttribute("user",users);  //使用setAttribute()方法,将获取的对象放入session中
                                                    // 在其他类中可以使用getAttribute()方法获取session中对象,一个前端只有一个session
            bean = JsonUtils.createJsonBean(1, users);
        }catch (Exception e){
            bean = JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }



    //注册
    @RequestMapping("/register.do")
    @ResponseBody
    public JsonBean regiest(SysUser user){
        JsonBean bean=null;

        try {
            uerService.addUser(user);
            bean = JsonUtils.createJsonBean(1, null);
        }catch (Exception e){
            bean = JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }

    //显示个人信息
    @RequestMapping("/denglu.do")
    @ResponseBody
    public JsonBean findUser(HttpSession session) {
        JsonBean bean = null;

        SysUser user1 = (SysUser) session.getAttribute("user");
        if (user1 == null) {
            bean = JsonUtils.createJsonBean(0,0);
        } else {
            bean = JsonUtils.createJsonBean(1,user1);
        }
        return bean;
    }


    //修改手机号
    @RequestMapping("/update1.do")
    @ResponseBody
    public JsonBean updateTel(String tel,HttpSession session){
        JsonBean bean=null;
        SysUser user1 = (SysUser)session.getAttribute("user");
        user1.setTel(tel);

        try {
            uerService.updateUserTel(user1);
            bean= JsonUtils.createJsonBean(1, "手机号修改成功");
        }catch (Exception e){
            e.printStackTrace();
            bean= JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }


    //修改邮箱
    @RequestMapping("/update2.do")
    @ResponseBody
    public JsonBean updateEmail(String email,HttpSession session){
        JsonBean bean=null;
        SysUser user2 = (SysUser)session.getAttribute("user");
        user2.setEmail(email);

        try {
            uerService.updateUserEmail(user2);
            bean= JsonUtils.createJsonBean(1, "邮箱修改成功");
        }catch (Exception e){
            e.printStackTrace();
            bean= JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }


    //修改密码
    @RequestMapping("/update3.do")
    @ResponseBody
    public JsonBean updatePassword(String password,HttpSession session){
        JsonBean bean=null;
        SysUser user3 = (SysUser)session.getAttribute("user");
        user3.setPassword(password);

        try {
            uerService.updateUserPassword(user3);
            bean= JsonUtils.createJsonBean(1, "密码修改成功");
        }catch (Exception e){
            e.printStackTrace();
            bean= JsonUtils.createJsonBean(0, e.getMessage());
        }
        return bean;
    }

}
